package com.jframe.sys.controller.utils;

import java.io.File;
import java.io.FileInputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import com.jframe.sys.utils.*;
public class ActionMap {
		
private static long lastModifiedTime = 0;
	
	private static Map classCache=Collections.synchronizedMap(new HashMap());
	private static Map<String,String[]> webRights= new HashMap<String,String[]>();
	private static Map<String,ActionPage> actionPages= new HashMap<String,ActionPage>();
	
	public static void loadAction(){
		load.load();
	}
	
	public static Map getClassCache()
	{
		if(classCache.isEmpty()){
			load.load();
		}
		return classCache;
	}
	
	public static Map getWebRights(){
		return  webRights;
	}
	
	public static ActionPage getActionPage(String actionName){
		return actionPages.get(actionName);
	}
	
	public static boolean hasWebRight(String action,String method){
		
		String [] rights = webRights.get(action);
		if(rights!=null){
			for(String right :rights){
				if(method.equals(right.trim())){
					return true;
				}
			}
		}
		return false;
	}
	
	static class load{
		
		static void load(){			
			new load().loadAll();
		}
		void loadAll(){
			
			String[] files= this.findAllActionProperties();
			for(String file:files){
				this.loadProperties(file);
			}
		}
		 
		String[] findAllActionProperties(){
			String path = StringUtil.getPrjAbsolutePath() + "WEB-INF/classes/"; 
			String[] files = FileUtil.getFileList(path, "action-config-*.properties");			
			return files;
		}
		
		 void loadProperties(String file ){
			String path = StringUtil.getPrjAbsolutePath() + "WEB-INF/classes/"+file;
			PropertiesUtil props = PropertiesUtil.getInstance(path);
			Map<String,String> map = props.loadAll();
			setClassCache(map);
		}
		
		 void setClassCache(Map<String,String> map){
			Set<String> keySet  =map.keySet();
			Iterator<String> it =keySet.iterator();
			while(it.hasNext())
			{
				String key =StringUtil.null2Space( it.next()).trim();
				try{
					String actionCfg = map.get(key);
					String[] cfg =  StringUtil.split( actionCfg,"|");
					String pz = "";
					if(cfg.length >2){
						pz= key + "=" + cfg[0] + "|" + cfg[1] + "|" + cfg[2];
					}
					else if(cfg.length >1){
						pz=key + "=" + cfg[0] + "|" + cfg[1] ;
					}
					else{
						pz=key + "=" + cfg[0] ;
					}
										
					Class clz = Class.forName(cfg[0]);				
					classCache.put(key,clz );
					
					if(cfg.length ==2){
						if(cfg[1].indexOf("}") >0){							
							ActionPage actionPage = (ActionPage) JsonUtil.json2Obj(cfg[1], ActionPage.class);
							actionPages.put(key, actionPage);
						}
						else{
							webRights.put(key,  StringUtil.split(cfg[1],","));						
						}					
					}
					else if(cfg.length==3){
						if(cfg[1].indexOf("}") >0){
							ActionPage actionPage = (ActionPage) JsonUtil.json2Obj(cfg[1], ActionPage.class);
							actionPages.put(key, actionPage);
							webRights.put(key,  StringUtil.split(cfg[2],","));
						}
						else{
							ActionPage actionPage = (ActionPage) JsonUtil.json2Obj(cfg[2], ActionPage.class);
							actionPages.put(key, actionPage);
							webRights.put(key,  StringUtil.split(cfg[1],","));
						}
					}
				}
				catch(Exception ex)
				{
					//ex.printStackTrace();
					LogHandler.error(key + "对应的类不存在！");
				}
			}
		}
		
		static void load(String configFilePath){
						
			//从action-config文件中获取action类，并将这些类信息。			
			File configFile = new File(configFilePath);
			lastModifiedTime = configFile.lastModified();
			if (lastModifiedTime == 0) {
				System.err.println(configFilePath + " 文件不存在!");
				return;
			}
			Properties props = new Properties();
			try {
				props.load(new FileInputStream(configFilePath));
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			Set keySet  =props.keySet();
			
			Iterator it =keySet.iterator();
			
			while(it.hasNext())
			{
				String key =StringUtil.null2Space( it.next()).trim();
				try{
					Class clz = Class.forName( props.getProperty(key));
					classCache.put(key,clz );
				}
				catch(Exception ex)
				{
					LogHandler.error(key + "对应的类不存在！");
				}
			}
			
			/*//从数据库配置中获取action
			InitLoadTableDAO service = new InitLoadTableDAO();
			List<SysAction> ls = service.findSysActionList();
			for(SysAction action :ls){
				
				try{
					Class clz = Class.forName( action.getClz());
					//System.out.println(action.getAction() + ":" + clz);
					classCache.put(action.getAction(),clz );
				}
				catch(Exception ex)
				{
					System.out.println(action.getAction() + "对应的类不存在！");
				}
			}*/
		}
	//}
	}
}
